package com.chap1;

import com.chap1.tools.SleepTools;

/**
 * <p>
 * <code>TestIntegerSyn</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-03-24 15:45
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class TestIntegerSyn2 {
    private Integer i = 0;
    public void incCountObj(){
        synchronized (i){
            i++;
        }
    }

    private static class Count extends Thread{
        private TestIntegerSyn2 testIntegerSyn2;

        public Count(TestIntegerSyn2 testIntegerSyn2) {
            this.testIntegerSyn2 = testIntegerSyn2;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10000; i++) {
                testIntegerSyn2.incCountObj();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestIntegerSyn2 testIntegerSyn2 = new TestIntegerSyn2();
        Count count1 = new Count(testIntegerSyn2);
        count1.start();
        Count count2 = new Count(testIntegerSyn2);
        count2.start();
        Thread.sleep(50);
        System.out.println(testIntegerSyn2.i);
    }
}
